<?php

class model_categories extends CI_Model {
    
    function __construct() {
        parent::__construct();
    }
    
    public function getMenu() {
    	$query = $this->db->query("SELECT * FROM categories");
        $list = [];
        foreach ($query->result() as $row) {
        	array_push($list, $row);
        }
        return $list;
    }

    public function getCategoryName($catId) {
        $where = $this->db->where("id_cat", $catId);
        $q = $this->db->get("categories");
        $cat = $q->row_array();
        return $cat['name_cat'];
    }

    public function catBooks($catId) {
        $this->db->where("parent_cat", $catId);
        $q = $this->db->get("categories");
        
        $list=[];
        array_push($list, $catId);
        foreach ($q->result_array() as $cat) {
            array_push($list, $cat['id_cat']);
        }

        $cats = join('","',$list);
        $cats = '"'.$cats; $cats = $cats.'"';

        $sql = "SELECT * FROM document as doc, categories as cat 
            WHERE doc.category = cat.id_cat AND cat.id_cat IN ($cats)";
        $q = $this->db->query($sql);

        return $q->result_array();
    }
/**
 * ================================================================
*/
}
?>